﻿using System;
using System.Web.Mvc;
using iss.sg4210.se18.team6s.jass.BL;
using iss.sg4210.se18.team6s.jass.DAL;
using iss.sg4210.se18.team6s.Utilities;

namespace iss.sg4210.se18.team6s.Controllers
{
    [Authorize(Roles = ViewConstants.ROLE_ADMIN + "," + ViewConstants.ROLE_ADVERTISER)]
    public class AdvertisementController : Controller
    {
        AdvertisementFacade advertisementFacade = null;
        CompanyFacade companyFacade = null;
        GeoAreaFacade geoAreaFacade = null;
        public AdvertisementController()
        {
            advertisementFacade = new AdvertisementFacade();
            companyFacade = new CompanyFacade();
            geoAreaFacade = new GeoAreaFacade();
        }
        //
        // GET: /Advertisement/

        public ActionResult Index()
        {
            if (Common.IsLoggedInUserAdmin())
                return View(advertisementFacade.FindAllAdvertisements());
            else
                return View(advertisementFacade.FindAllAdvertisementsByUser(Common.GetLoggedInUserId()));
        }

        //
        // GET: /Advertisement/Details/5

        public ActionResult Details(int id)
        {
            Advertisement advertisement = advertisementFacade.GetAdvertisement(id);
            if (!Common.IsLoggedInUserAdmin() && !Common.IsLoggedInUserAdvertiser())
            {
                //add view log
                AdvertisementViewLog viewLog = new AdvertisementViewLog();
                viewLog.AdvertisementId = advertisement.Id;
                viewLog.UserId =  Common.GetLoggedInUserId();
                viewLog.AdvertisementViewTimeStamp = DateTime.Now;

                advertisementFacade.AddAdvertisementViewLog(viewLog);
            }
            return View(advertisement);
        }

        //
        // GET: /Advertisement/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Advertisement/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                Advertisement advertisement = new Advertisement();
                UpdateModel(advertisement, collection);
                advertisement.UserId = Common.GetLoggedInUserId();
                advertisementFacade.AddAdvertisement(advertisement);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /Advertisement/Edit/5

        public ActionResult Edit(int id)
        {
            Advertisement advertisement = advertisementFacade.GetAdvertisement(id);
            return View(advertisement);
        }

        //
        // POST: /Advertisement/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            Advertisement advertisement = advertisementFacade.GetAdvertisement(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(advertisement);
                advertisementFacade.UpdateAdvertisement(advertisement);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertisement);
            }
            catch (Exception ex)
            {
                return View(advertisement);
            }
        }

        //
        // GET: /Advertisement/Delete/5

        public ActionResult Delete(int id)
        {
            Advertisement advertisement = advertisementFacade.GetAdvertisement(id);
            return View(advertisement);
        }

        //
        // POST: /Advertisement/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                advertisementFacade.DeleteAdvertisement(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertisementFacade.GetAdvertisement(id));
            }
            catch (Exception ex)
            {
                return View(advertisementFacade.GetAdvertisement(id));
            }
        }

        public ActionResult AdvertisementsCompanyList(int id)
        {
            if (id < 1)
                ViewData[ViewConstants.VIEW_DATA_COMPANY_LIST] = new SelectList(companyFacade.FindAllCompanies(), "Id", "Name");
            else
                ViewData[ViewConstants.VIEW_DATA_COMPANY_LIST] = new SelectList(companyFacade.FindAllCompanies(), "Id", "Name", advertisementFacade.GetCompanyByAdvertisementId(id).Id);

            return View("CompanyDropDownList");
        }

        public ActionResult AdvertisementsGeoAreaList(int id)
        {
            if (id < 1)
                ViewData[ViewConstants.VIEW_DATA_LOCATION_LIST] = new SelectList(geoAreaFacade.FindAllGeographicalAreas(), "Id", "Name");
            else
                ViewData[ViewConstants.VIEW_DATA_LOCATION_LIST] = new SelectList(geoAreaFacade.FindAllGeographicalAreas(), "Id", "Name", advertisementFacade.GetGeographicalAreaByAdvertisementId(id).Id);

            return View("GeoAreaDropDownList");
        }

        public ActionResult AdvertisementsAdvertisementTypeList(int id)
        {
            if (id < 1)
                ViewData[ViewConstants.VIEW_DATA_ADVERTISEMENT_TYPE_LIST] = new SelectList(advertisementFacade.FindAllAdvertisementTypes(), "Id", "Type");
            else
                ViewData[ViewConstants.VIEW_DATA_ADVERTISEMENT_TYPE_LIST] = new SelectList(advertisementFacade.FindAllAdvertisementTypes(), "Id", "Type", advertisementFacade.GetAdvertisementTypeByAdvertisementId(id).Id);

            return View("AdvertisementTypeDropDownList");
        }
    }

    [Authorize(Roles = ViewConstants.ROLE_ADMIN)]
    public class AdvertisementTypeController : Controller
    {
        AdvertisementFacade advertisementFacade = null;
        public AdvertisementTypeController()
        {
            advertisementFacade = new AdvertisementFacade();
        }
        //
        // GET: /AdvertisementType/

        public ActionResult Index()
        {
            return View(advertisementFacade.FindAllAdvertisementTypes());
        }

        //
        // GET: /AdvertisementType/Details/5

        public ActionResult Details(int id)
        {
            AdvertisementType advertisementType = advertisementFacade.GetAdvertisementType(id);
            return View(advertisementType);
        }

        //
        // GET: /AdvertisementType/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /AdvertisementType/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                AdvertisementType advertisementType = new AdvertisementType();
                UpdateModel(advertisementType, collection);
                advertisementFacade.AddAdvertisementType(advertisementType);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View();
            }
            catch (Exception ex)
            {
                return View();
            }
        }

        //
        // GET: /AdvertisementType/Edit/5

        public ActionResult Edit(int id)
        {
            AdvertisementType advertisementType = advertisementFacade.GetAdvertisementType(id);
            return View(advertisementType);
        }

        //
        // POST: /AdvertisementType/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            AdvertisementType advertisementType = advertisementFacade.GetAdvertisementType(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(advertisementType);
                advertisementFacade.UpdateAdvertisementType(advertisementType);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertisementType);
            }
            catch (Exception ex)
            {
                return View(advertisementType);
            }
        }

        //
        // GET: /AdvertisementType/Delete/5

        public ActionResult Delete(int id)
        {
            AdvertisementType advertisementType = advertisementFacade.GetAdvertisementType(id);
            return View(advertisementType);
        }

        //
        // POST: /AdvertisementType/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                advertisementFacade.DeleteAdvertisementType(id);
                return RedirectToAction("Index");
            }
            catch (JASSException ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
                return View(advertisementFacade.GetAdvertisementType(id));
            }
            catch (Exception ex)
            {
                return View(advertisementFacade.GetAdvertisementType(id));
            }
        }
    }
}
